package org.springframework.amqp.tutorials.config.direct;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

@Component
@RabbitListener(queues = "myDirectQueue")
@Slf4j
public class DirectReceiver {

    @RabbitHandler
    public void process(String msg, Channel channel, Message message) throws IOException, InterruptedException {
        channel.basicQos(1);
        TimeUnit.SECONDS.sleep(2);
        System.out.println("receiver2: " + msg);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        log.info("第1个消费者收到消息：" + msg);
    }

}